package vikaa.ui;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import vikaa.tools.DialogService;
import vikaa.tools.HttpUtil;
import vikaa.tools.StringUtils;
import vikaa.tools.ValueClass;
import vikaa.wowo.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Feedback extends Activity{
	private final static String TAG = "Feedback";
	private LinearLayout backBtn;
	private ImageView sendBtn;
	private EditText feedEdt;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.feedback);
		initview();
	}
	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		switch(id){
		case 1:
			final EditText input = new EditText(this);
			return new AlertDialog.Builder(this)
			.setTitle("窝窝")
			.setMessage("谢谢您的反馈")
			.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener(){
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			})
			.create();
		default:
			return null;
		}
	}
	private void initview(){
		backBtn = (LinearLayout)findViewById(R.id.backBtn);
		backBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				DialogService.hideSoftKeypad(Feedback.this);
				finish();
			}
		});
		sendBtn = (ImageView)findViewById(R.id.submitBtn);
		sendBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				DialogService.hideSoftKeypad(Feedback.this);
				String content = feedEdt.getText().toString();
				content = StringUtils.spaceEdit(content);
				if (content.length() < 1 ) {
					DialogService.WarnDialog(Feedback.this, getString(R.string.tip), getString(R.string.tips_comment_null), getString(R.string.ok));
				}
				else {
					new FeedbackPostThread(content, ValueClass.FEEDBACK_SOURCE, 1).start();
				}
			}
		});
		feedEdt = (EditText)findViewById(R.id.feedEdt);
	}
	 Handler mhandler = new Handler(){
			public void handleMessage(Message msg) {
				switch(msg.what){
				case 1:
					showDialog(1);
					break;
				case 3:
					if(msg.obj != null){
						Toast.makeText(Feedback.this, (String)msg.obj, Toast.LENGTH_SHORT).show();
					}
					else
						Toast.makeText(Feedback.this, R.string.server_crash, Toast.LENGTH_SHORT).show();
					break;
				case 4:
					Toast.makeText(Feedback.this, R.string.network_crash, Toast.LENGTH_SHORT).show();
					break;
				}
			}
	    };
	class FeedbackPostThread extends Thread{
    	private String text;
    	private String source;
    	private int m;
    	boolean isrun = true;
		public FeedbackPostThread(String text, String source, int m) {
			super();
			this.text = text;
			this.source = source;
			this.m = m;
		}
    	public void run() {
    		while(isrun){
    			isrun = false;
    			Map<String, String> map = new HashMap<String, String>();
    			map.put("source", source);
    			map.put("message", text);
    			try {
					JSONObject jsonObj = new JSONObject(HttpUtil.postRequest(HttpUtil.BASE_URL+"feedback/send", map));
					if(jsonObj.getString("result").equals("1")){
						Message msg = mhandler.obtainMessage(1);
						msg.sendToTarget();
					}
					else if(jsonObj.getString("result").equals("0")){
						Message msg = mhandler.obtainMessage(3);
						msg.obj = jsonObj.getString("msg");
						msg.sendToTarget();
					}
					else {
						Message msg = mhandler.obtainMessage(4);
						msg.sendToTarget();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
    		}
    	}
    }
}
